// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.1

package file

import (
	"audio-server/internal/result"
	"net/http"

	"audio-server/internal/logic/file"
	"audio-server/internal/svc"
	"audio-server/internal/types"

	"github.com/zeromicro/go-zero/rest/httpx"
)

func FileUploadHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.FileUploadReq
		if err := httpx.Parse(r, &req); err != nil {
			httpx.OkJsonCtx(r.Context(), w, result.Fail(err.Error()))
			return
		}
		formFile, header, err := r.FormFile("file")
		if err != nil {
			httpx.OkJsonCtx(r.Context(), w, result.Fail(err.Error()))
			return
		}
		defer formFile.Close()

		l := file.NewFileUploadLogic(r.Context(), svcCtx)
		resp, err := l.FileUpload(formFile, header)
		if err != nil {
			httpx.OkJsonCtx(r.Context(), w, err)
		} else {
			httpx.OkJsonCtx(r.Context(), w, resp)
		}
	}
}
